<?php
/**
 * This file defines the PHP class {@see \BF\XmlAttributeHelper}.
 *
 * @author   Kado <ulfikado@gmail.com>
 * @category Beluga PHP5.3+ Framework
 * @package  BF\Core
 * @since    2015-01-17 13:50
 * @version  0.1  
 */

namespace BF
{

   /**
    * This class "XmlAttributeHelper" defines a object that have some static methods for handling XML attributes
    *
    * @since v0.1
    */
   class XmlAttributeHelper
   {

      /**
       * Returns the XML attribute value of the defined XML element, or $defaultValue if the attribute not exists.
       *
       * @param  \SimpleXMLElement $element       The XML element
       * @param  string            $attributeName The name of the required XML attribute
       * @param  mixed             $defaultValue  The default value, returned if the attribute not exists.
       * @return string
       */
      public static function GetAttributeValue( \SimpleXMLElement $element, $attributeName, $defaultValue = false )
      {

         if ( isset( $element[ $attributeName ] ) )
         {
            return (string) $element[ $attributeName ];
         }

         if ( isset( $element[ 'attributes' ][ $attributeName ] ) )
         {
            return (string) $element[ 'attributes' ][ $attributeName ];
         }

         if ( isset( $element[ '@attributes' ][ $attributeName ] ) )
         {
            return (string) $element[ '@attributes' ][ $attributeName ];
         }

         $attr = $element->attributes();

         if ( isset( $attr[ $attributeName ] ) )
         {
            return (string) $attr[ $attributeName ];
         }

         return $defaultValue;

      }

   }

}

 